package com.ddwl.wallet.dao;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.DistConstant;
import com.ddwl.common.dao.TkDao;
import com.ddwl.wallet.dao.mapper.OrderInfoMapper;
import com.ddwl.wallet.dao.model.DistOrder;
import com.ddwl.wallet.dao.model.DistOrderInfo;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.List;


@Repository
public class DistOrderInfoDao extends TkDao<OrderInfoMapper, DistOrderInfo> {


    /**
     * 查询根据订单号查询分销订单
     * @param orderId
     * @return
     */
    public DistOrderInfo queryDistOrderByOrderId(String orderId){
        DistOrderInfo distOrder = new DistOrderInfo();
        distOrder.setOrderId(orderId);
        return selectOne(distOrder);
    }


    /**
     * 查询收货时间大于指定时间的订单指定时间可结算的店铺订单
     * @return
     */
    public List<DistOrderInfo> queryGtTimeSettleDistOrder(Instant startTime,Instant endTime){
        Example example = getExample();
        example.and().andEqualTo("orderStatus", DistConstant.DistOrderStatus.TAKE.getCode())
                .andNotEqualTo("settlementStatus", BaseEnum.ApplyEnum.SUCCEED.getCode())
                .andEqualTo("type",0)
                .andGreaterThanOrEqualTo("confirmTime", startTime)
                .andLessThan("confirmTime",endTime);
        return baseMapper.selectByExample(example);
    }

    /**
     * 查询收货时间大于指定时间的开店订单指定时间可结算的开通店铺订单
     * @return
     */
    public List<DistOrderInfo> queryGtTimeShopSettleDistOrder(Instant startTime,Instant endTime){
        Example example = getExample();
        example.and().andEqualTo("orderStatus", DistConstant.DistOrderStatus.TAKE.getCode())
                .andNotEqualTo("settlementStatus", BaseEnum.ApplyEnum.SUCCEED.getCode())
                .andEqualTo("type",1)
                .andGreaterThanOrEqualTo("confirmTime", startTime)
                .andLessThan("confirmTime",endTime);
        return baseMapper.selectByExample(example);
    }


    /**
     * 查询收货时间大于指定时间的订单指定时间可结算的分销订单
     * @return
     */
    public List<DistOrderInfo> queryGtTimeSettleShopOrder(Instant startTime,Instant endTime){
        Example example = getExample();
        example.and().andEqualTo("orderStatus", DistConstant.DistOrderStatus.TAKE.getCode())
                .andNotEqualTo("shopSettlementStatus", BaseEnum.ApplyEnum.SUCCEED.getCode())
                .andEqualTo("type",0)
                .andEqualTo("platformTag", 1)
                .andBetween("confirmTime",startTime,endTime);
        return baseMapper.selectByExample(example);
    }

    /**
     * 订单确认收货
     * @param orderId
     * @param confirmTime
     */
    public void confirmOrder(String orderId,Instant confirmTime){
        DistOrderInfo distOrder = new DistOrderInfo();
        distOrder.setOrderStatus(DistConstant.DistOrderStatus.TAKE.getCode());
        distOrder.setConfirmTime(confirmTime);
        distOrder.setGmtModified(Instant.now());
        Example example = new Example(DistOrder.class);
        example.and().andEqualTo("orderId",orderId);
        baseMapper.updateByExampleSelective(distOrder,example);
    }


    public void addNumberRefund(String orderId, BigDecimal refundAmount){
        baseMapper.addNumberRefund(orderId,refundAmount);
    }

    public void minusNumberRefund(String orderId, BigDecimal refundAmount){
        baseMapper.minusNumberRefund(orderId,refundAmount);
    }
}
